import { createStore } from 'vuex'

export default createStore({
  state: {
    user: {
      id: '',
      name: '',
      domain: '',
      avatar: '',
      resourcesList: []
    }
  },
  mutations: {
    SET_USER (state, user) {
      state.user = user
    }
  },
  actions: {
    submitUser ({ commit }, user) {
      commit('SET_USER', user)
      localStorage.setItem('user', JSON.stringify(user))
    },
    logout ({ commit }) {
      commit('SET_USER', {
        id: ''
      })
      localStorage.removeItem('user')
    }
  },
  modules: {},
  getters: {
    user: state => state.user,
    manageRouteList: state => {
      const resourcesList = state.user.resourcesList
      let manageMenuList = []
      if (resourcesList && resourcesList.length > 0) {
        resourcesList.forEach(item => {
          if (item.vueMark === 'manage') {
            manageMenuList = item.childList
          }
        })
      }
      return manageMenuList
    }
  }
})
